package com.lemon.core.common.lang.regex;

import java.util.regex.Pattern;

/**
 * Class Name: CommonRegxUtils
 * Creator: lottery
 * Version: v1.0
 * Updater:
 * Date Time:
 * Description:
 * <p>
 * 常用正则匹配
 */
public class CommonRegxUtils {
    /**
     * 正则表达式：验证手机号
     */
    private static final String REGEX_MOBILE = "^(13|14|15|16|17|18|19)[0-9]{9}$";

    /**
     * 正则表达式：验证邮箱
     */
    private static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证汉字
     */
    private static final String REGEX_CHINESE = "[\u4e00-\u9fa5]";

    /**
     * 正则表达式：验证身份证
     */
    private static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";

    /**
     * 正则表达式：验证URL
     */
    private static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";

    /**
     * 正则表达式：验证IP地址
     */
    private static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";

    private static final Pattern IS_CHINESE_PATTERN = Pattern.compile(REGEX_CHINESE);
    private static final Pattern IS_ID_CARD_PATTERN = Pattern.compile(REGEX_ID_CARD);

    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isMobile(String mobile) {
        return mobile != null && Pattern.matches(REGEX_MOBILE, mobile);
    }

    public static boolean isMobile(String mobile, boolean ignoreBuildin) {
        return Pattern.matches(REGEX_MOBILE, mobile) || (ignoreBuildin && mobile.startsWith("117") && mobile.length() == 11);
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isEmail(String email) {
        return Pattern.matches(REGEX_EMAIL, email);
    }

    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true，否则返回false
     */
    public static boolean containChinese(String chinese) {
        return IS_CHINESE_PATTERN.matcher(chinese).find();
    }

    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true，否则返回false
     */
    public static boolean isUrl(String url) {
        return Pattern.matches(REGEX_URL, url);
    }

    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }

}
